package com.dqhl.businessapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.dqhl.businessapp.R;
import com.dqhl.businessapp.base.BaseActivity;
import com.dqhl.businessapp.model.User;
import com.dqhl.businessapp.util.Dlog;
import com.dqhl.businessapp.util.UserSaveUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by mac on 16/11/24.
 * 登陆
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private EditText et_account;
    private EditText et_password;
    private Button btn_pwd_show;
    private Button btn_login;
    private boolean isHidden = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        context = this;

        initView();
        initListener();
        isLogin();
    }

    private void isLogin() {
        if (user != null) {
            Dlog.e("login:" + user.toString());
            forward(MainActivity.class);
        }
    }

    private void initView() {
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_pwd_show = (Button) findViewById(R.id.btn_pwd_show);
        btn_login = (Button) findViewById(R.id.btn_login);
    }

    private void initListener() {
        btn_pwd_show.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_pwd_show:
                if (isHidden) {
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btn_pwd_show.setBackgroundResource(R.drawable.ic_pwd_show);
                } else {
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btn_pwd_show.setBackgroundResource(R.drawable.ic_pwd_hide);
                }
                isHidden = !isHidden;
                et_password.postInvalidate();
                //切换后将EditText光标置于末尾
                CharSequence charSequence = et_password.getText();
                if (charSequence instanceof Spannable) {
                    Spannable spanText = (Spannable) charSequence;
                    Selection.setSelection(spanText, charSequence.length());
                }
                break;
            case R.id.btn_login:
                doLogin();
                break;
        }
    }

    /**
     * 登陆
     */
    private void doLogin() {
        String account = et_account.getText().toString();
        String password = et_password.getText().toString();
        if (TextUtils.isEmpty(account)) {
            toast("账号不能为空");
        } else if (TextUtils.isEmpty(password)) {
            toast("账号不能为空");
        } else {
            showLoadingBar();
            User u = new User();
            u.setId(account);
            UserSaveUtil.saveUser(context, u);
            new Timer(true).schedule(new TimerTask() {
                @Override
                public void run() {
                    forward(MainActivity.class);
                    hideLoadingBar();
                }
            }, 1500);
        }
    }


}
